package com.mallcloud.mall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.constant.CacheConstants;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.product.api.entity.BrandModel;
import com.mallcloud.mall.product.service.BrandService;
import org.springframework.cache.CacheManager;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "品牌信息管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/product/brand")
public class BrandController {
    private final BrandService brandService;
    private final CacheManager cacheManager;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('product_brand_view')")
    public R<IPage<BrandModel>> page(Page page, BrandModel dto) {
        return R.ok(brandService.page(page, Wrappers.query(dto)));
    }

    @ApiOperation("列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('product_brand_view')")
    public R<List<BrandModel>> list(BrandModel dto) {
        return R.ok(brandService.list(Wrappers.query(dto)));
    }

    @ApiOperation("通过id查询")
    @GetMapping("/getInfo")
    @PreAuthorize("@pms.hasPermission('product_brand_view')")
    public R<BrandModel> getInfo(BrandModel dto) {
        return R.ok(brandService.getById(dto.getBrandId()));
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('product_brand_add')")
    public R<Boolean> save(@RequestBody BrandModel dto) {
        cacheManager.getCache(CacheConstants.PROJECT_BRAND_TREE).clear();
        return R.ok(brandService.save(dto));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('product_brand_edit')")
    public R<Boolean> updateById(@RequestBody BrandModel dto) {
        cacheManager.getCache(CacheConstants.PROJECT_BRAND_TREE).clear();
        return R.ok(brandService.updateById(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{brandId}")
    @PreAuthorize("@pms.hasPermission('product_brand_del')")
    public R<Boolean> removeById(@PathVariable Integer brandId) {
        cacheManager.getCache(CacheConstants.PROJECT_BRAND_TREE).clear();
        return R.ok(brandService.removeById(brandId));
    }

    @ApiOperation("批量删除品牌信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('product_brand_del')")
    public R<Boolean> removeByIds(@RequestBody List<Long> brandIds) {
        cacheManager.getCache(CacheConstants.PROJECT_BRAND_TREE).clear();
        return R.ok(brandService.removeByIds(brandIds));
    }

    @ApiOperation("导出")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('product_brand_export')")
    public List<BrandModel> export(BrandModel brand) {
        return brandService.list(Wrappers.query(brand));
    }
}
